<title>查询页面</title>
<!-- 
本「综合演示」包含：自定义头部工具栏、获取表格数据、表格重载、自定义模板、单双行显示、单元格编辑、自定义底部分页栏、表格相关事件与操作、与其他组件的结合等相对常用的功能，以便快速掌握 table 组件的使用。
-->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">表格示例</div>
        <div class="layui-card-body">
            <table class="layui-hide" id="test" lay-filter="test"></table>
        </div>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
    <button class="layui-btn layui-btn-sm" lay-event="getData">获取当前页数据</button>
    <button class="layui-btn layui-btn-sm" id="dropdownButton">
      下拉按钮 
      <i class="layui-icon layui-icon-down layui-font-12"></i>
    </button>
    <button class="layui-btn layui-btn-sm layui-bg-blue" id="reloadTest">
      重载测试 
      <i class="layui-icon layui-icon-down layui-font-12"></i>
    </button>
    <button class="layui-btn layui-btn-sm layui-btn-primary" id="rowMode">
      <span>{{= d.lineStyle ? '多行' : '单行' }}模式</span>
      <i class="layui-icon layui-icon-down layui-font-12"></i>
    </button>
  </div>
</script>
<script type="text/html" id="barDemo">
  <div class="layui-clear-space">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="more">
      更多 
      <i class="layui-icon layui-icon-down"></i>
    </a>
  </div>
</script>
<script>
    layui.use(['table', 'dropdown'], function () {
        var table = layui.table;
        var dropdown = layui.dropdown;
        var tabledata = {
            "code": 0
            , "msg": ""
            , "count": 1000
            , "totalRow": {
                "era": {
                    "tang": "2",
                    "song": "2",
                    "xian": "20"
                }
            }
            , "data": [{
                "id": "10001"
                , "username": "李白"
                , "email": "test1@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "君不见，黄河之水天上来，奔流到海不复回。 君不见，高堂明镜悲白发，朝如青丝暮成雪。 人生得意须尽欢，莫使金樽空对月。 天生我材必有用，千金散尽还复来。 烹羊宰牛且为乐，会须一饮三百杯。 岑夫子，丹丘生，将进酒，杯莫停。 与君歌一曲，请君为我倾耳听。(倾耳听 一作：侧耳听) 钟鼓馔玉不足贵，但愿长醉不复醒。(不足贵 一作：何足贵；不复醒 一作：不愿醒/不用醒) 古来圣贤皆寂寞，惟有饮者留其名。(古来 一作：自古；惟 通：唯) 陈王昔时宴平乐，斗酒十千恣欢谑。 主人何为言少钱，径须沽取对君酌。 五花马，千金裘，呼儿将出换美酒，与尔同销万古愁。"
                , "experience": "12"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10002"
                , "username": "杜甫"
                , "email": "test2@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "舍南舍北皆春水，但见群鸥日日来。花径不曾缘客扫，蓬门今始为君开。盘飧市远无兼味，樽酒家贫只旧醅。肯与邻翁相对饮，隔篱呼取尽余杯。"
                , "experience": "116"
                , "ip": "192.168.0.8"
                , "checkin": "108"
                , "joinTime": "2016-10-14"
                , "LAY_CHECKED": true
            }, {
                "id": "10003"
                , "username": "苏轼"
                , "email": "test3@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "大江东去，浪淘尽，千古风流人物。故垒西边，人道是，三国周郎赤壁。乱石穿空，惊涛拍岸，卷起千堆雪。江山如画，一时多少豪杰。遥想公瑾当年，小乔初嫁了，雄姿英发。羽扇纶巾，谈笑间，樯橹灰飞烟灭。故国神游，多情应笑我，早生华发。人生如梦，一尊还酹江月。"
                , "experience": "65"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10004"
                , "username": "李清照"
                , "email": "test4@email.com"
                , "sex": "女"
                , "city": "浙江杭州"
                , "sign": "昨夜雨疏风骤，浓睡不消残酒。试问卷帘人，却道海棠依旧。知否，知否？应是绿肥红瘦。"
                , "experience": "777"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10005"
                , "username": "冰心"
                , "email": "test5@email.com"
                , "sex": "女"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "86"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10006"
                , "username": "张三"
                , "email": "test6@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "12"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10007"
                , "username": "张三7"
                , "email": "test7@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "16"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10008"
                , "username": "张三8"
                , "email": "test8@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10009"
                , "username": "张三9"
                , "email": "test9@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10010"
                , "username": "张三10"
                , "email": "test10@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10011"
                , "username": "张三11"
                , "email": "test11@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10012"
                , "username": "张三12"
                , "email": "test12@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10013"
                , "username": "张三13"
                , "email": "test13@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10014"
                , "username": "张三14"
                , "email": "test14@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10015"
                , "username": "张三15"
                , "email": "test15@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10016"
                , "username": "张三16"
                , "email": "test16@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10017"
                , "username": "张三17"
                , "email": "test17@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10018"
                , "username": "张三18"
                , "email": "test18@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10019"
                , "username": "张三19"
                , "email": "test19@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10020"
                , "username": "张三20"
                , "email": "test20@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10021"
                , "username": "张三21"
                , "email": "test21@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10022"
                , "username": "张三22"
                , "email": "test22@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10023"
                , "username": "张三23"
                , "email": "test23@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }, {
                "id": "10024"
                , "username": "张三24"
                , "email": "test24@email.com"
                , "sex": "男"
                , "city": "浙江杭州"
                , "sign": "保持真善美，温和待人"
                , "experience": "106"
                , "ip": "192.168.0.8"
                , "checkin": "106"
                , "joinTime": "2016-10-14"
            }]
        }
        // 创建渲染实例
        table.render({
            elem: '#test',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            // height: 'full-180', // 最大高度减去其他容器已占有的高度差
            css: [ // 重设当前表格样式
                '.layui-table-tool-temp{padding-right: 145px;}'
            ].join(''),
            data: tabledata.data,
            cellMinWidth: 80,
            totalRow: true, // 开启合计行
            page: true,
            cols: [[
                { type: 'checkbox', fixed: 'left' },
                { field: 'id', fixed: 'left', width: 80, title: 'ID', sort: true, totalRowText: '合计：' },
                { field: 'username', width: 80, title: '用户' },
                { field: 'email', title: '邮箱 <i class="layui-icon layui-icon-tips layui-font-14" lay-event="email-tips" title="该字段开启了编辑功能" style="margin-left: 5px;"></i>', fieldTitle: '邮箱', hide: 0, width: 150, edit: 'text' },
                { field: 'sex', width: 80, title: '性别', sort: true },
                { field: 'sign', title: '签名', edit: 'textarea', minWidth: 260, expandedWidth: 260, style: '-webkit-box-align: start;' },
                { field: 'experience', width: 100, title: '积分', sort: true, totalRow: '{{ d.TOTAL_NUMS }} ' },
                { field: 'checkin', title: '打卡', width: 100, sort: true, totalRow: '{{ parseInt(d.TOTAL_NUMS) }} 次' },
                { field: 'ip', title: 'IP', width: 120 },
                { field: 'joinTime', title: '加入时间', width: 120 },
                { fixed: 'right', title: '操作', width: 134, minWidth: 125, toolbar: '#barDemo' }
            ]],
            done: function () {
                var id = this.id;
                // 下拉按钮测试
                dropdown.render({
                    elem: '#dropdownButton', // 可绑定在任意元素中，此处以上述按钮为例
                    data: [{
                        id: 'add',
                        title: '添加'
                    }, {
                        id: 'update',
                        title: '编辑'
                    }, {
                        id: 'delete',
                        title: '删除'
                    }],
                    // 菜单被点击的事件
                    click: function (obj) {
                        var checkStatus = table.checkStatus(id)
                        var data = checkStatus.data; // 获取选中的数据
                        switch (obj.id) {
                            case 'add':
                                layer.open({
                                    title: '添加',
                                    type: 1,
                                    area: ['80%', '80%'],
                                    content: '<div style="padding: 16px;">自定义表单元素</div>'
                                });
                                break;
                            case 'update':
                                if (data.length !== 1) return layer.msg('请选择一行');
                                layer.open({
                                    title: '编辑',
                                    type: 1,
                                    area: ['80%', '80%'],
                                    content: '<div style="padding: 16px;">自定义表单元素</div>'
                                });
                                break;
                            case 'delete':
                                if (data.length === 0) {
                                    return layer.msg('请选择一行');
                                }
                                layer.msg('delete event');
                                break;
                        }
                    }
                });

                // 重载测试
                dropdown.render({
                    elem: '#reloadTest', // 可绑定在任意元素中，此处以上述按钮为例
                    data: [{
                        id: 'reload',
                        title: '重载'
                    }, {
                        id: 'reload-deep',
                        title: '重载 - 参数叠加'
                    }, {
                        id: 'reloadData',
                        title: '仅重载数据'
                    }, {
                        id: 'reloadData-deep',
                        title: '仅重载数据 - 参数叠加'
                    }],
                    // 菜单被点击的事件
                    click: function (obj) {
                        switch (obj.id) {
                            case 'reload':
                                // 重载 - 默认（参数重置）
                                table.reload('test', {
                                    where: {
                                        abc: '123456',
                                        //test: '新的 test2',
                                        //token: '新的 token2'
                                    },
                                    /*
                                    cols: [[ // 重置表头
                                      {type: 'checkbox', fixed: 'left'},
                                      {field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计：'},
                                      {field:'sex', title:'性别', width:80, edit: 'text', sort: true},
                                      {field:'experience', title:'积分', width:80, sort: true, totalRow: true, templet: '<div>{{= d.experience }} 分</div>'},
                                      {field:'logins', title:'登入次数', width:100, sort: true, totalRow: true},
                                      {field:'joinTime', title:'加入时间', width:120}
                                    ]]
                                    */
                                });
                                break;
                            case 'reload-deep':
                                // 重载 - 深度（参数叠加）
                                table.reload('test', {
                                    where: {
                                        abc: 123,
                                        test: '新的 test1'
                                    },
                                    //defaultToolbar: ['print'], // 重载头部工具栏右侧图标
                                    //cols: ins1.config.cols
                                }, true);
                                break;
                            case 'reloadData':
                                // 数据重载 - 参数重置
                                table.reloadData('test', {
                                    where: {
                                        abc: '123456',
                                        //test: '新的 test2',
                                        //token: '新的 token2'
                                    },
                                    scrollPos: 'fixed',  // 保持滚动条位置不变 - v2.7.3 新增
                                    height: 2000, // 测试无效参数（即与数据无关的参数设置无效，此处以 height 设置无效为例）
                                    //url: '404',
                                    //page: {curr: 1, limit: 30} // 重新指向分页
                                });
                                break;
                            case 'reloadData-deep':
                                // 数据重载 - 参数叠加
                                table.reloadData('test', {
                                    where: {
                                        abc: 123,
                                        test: '新的 test1'
                                    }
                                }, true);
                                break;
                        }
                        layer.msg('可观察 Network 请求参数的变化');
                    }
                });

                // 行模式
                dropdown.render({
                    elem: '#rowMode',
                    data: [{
                        id: 'default-row',
                        title: '单行模式（默认）'
                    }, {
                        id: 'multi-row',
                        title: '多行模式'
                    }],
                    // 菜单被点击的事件
                    click: function (obj) {
                        var checkStatus = table.checkStatus(id)
                        var data = checkStatus.data; // 获取选中的数据
                        switch (obj.id) {
                            case 'default-row':
                                table.reload('test', {
                                    lineStyle: null // 恢复单行
                                });
                                layer.msg('已设为单行');
                                break;
                            case 'multi-row':
                                table.reload('test', {
                                    // 设置行样式，此处以设置多行高度为例。若为单行，则没必要设置改参数 - 注：v2.7.0 新增
                                    lineStyle: 'height: 95px;'
                                });
                                layer.msg('即通过设置 lineStyle 参数可开启多行');
                                break;
                        }
                    }
                });
            },
            error: function (res, msg) {
                console.log(res, msg)
            }
        });

        // 工具栏事件
        table.on('toolbar(test)', function (obj) {
            var id = obj.config.id;
            var checkStatus = table.checkStatus(id);
            var othis = lay(this);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(layui.util.escape(JSON.stringify(data)));
                    break;
                case 'getData':
                    var getData = table.getData(id);
                    console.log(getData);
                    layer.alert(layui.util.escape(JSON.stringify(getData)));
                    break;
                case 'LAYTABLE_TIPS':
                    layer.alert('自定义工具栏图标按钮');
                    break;
            };
        });
        // 表头自定义元素工具事件 --- 2.8.8+
        table.on('colTool(test)', function (obj) {
            var event = obj.event;
            if (event === 'email-tips') {
                layer.alert(layui.util.escape(JSON.stringify(obj.col)), {
                    title: '当前列属性配置项'
                });
            }
        });

        // 触发单元格工具事件
        table.on('tool(test)', function (obj) { // 双击 toolDouble
            var data = obj.data; // 获得当前行数据
            // console.log(obj)
            if (obj.event === 'edit') {
                layer.open({
                    title: '编辑 - id:' + data.id,
                    type: 1,
                    area: ['80%', '80%'],
                    content: '<div style="padding: 16px;">自定义表单元素</div>'
                });
            } else if (obj.event === 'more') {
                // 更多 - 下拉菜单
                dropdown.render({
                    elem: this, // 触发事件的 DOM 对象
                    show: true, // 外部事件触发即显示
                    data: [{
                        title: '查看',
                        id: 'detail'
                    }, {
                        title: '删除',
                        id: 'del'
                    }],
                    click: function (menudata) {
                        if (menudata.id === 'detail') {
                            layer.msg('查看操作，当前行 ID:' + data.id);
                        } else if (menudata.id === 'del') {
                            layer.confirm('真的删除行 [id: ' + data.id + '] 么', function (index) {
                                obj.del(); // 删除对应行（tr）的DOM结构
                                layer.close(index);
                                // 向服务端发送删除指令
                            });
                        }
                    },
                    align: 'right', // 右对齐弹出
                    style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' // 设置额外样式
                })
            }
        });

        // 触发表格复选框选择
        table.on('checkbox(test)', function (obj) {
            console.log(obj)
        });

        // 触发表格单选框选择
        table.on('radio(test)', function (obj) {
            console.log(obj)
        });

        // 行单击事件
        table.on('row(test)', function (obj) {
            //console.log(obj);
            //layer.closeAll('tips');
        });
        // 行双击事件
        table.on('rowDouble(test)', function (obj) {
            console.log(obj);
        });

        // 单元格编辑事件
        table.on('edit(test)', function (obj) {
            var field = obj.field; // 得到字段
            var value = obj.value; // 得到修改后的值
            var data = obj.data; // 得到所在行所有键值
            // 值的校验
            if (field === 'email') {
                if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(obj.value)) {
                    layer.tips('输入的邮箱格式不正确，请重新编辑', this, { tips: 1 });
                    return obj.reedit(); // 重新编辑 -- v2.8.0 新增
                }
            }
            // 编辑后续操作，如提交更新请求，以完成真实的数据更新
            // …
            layer.msg('编辑成功', { icon: 1 });

            // 其他更新操作
            var update = {};
            update[field] = value;
            obj.update(update);
        });
    });
</script>